package net.zjitc.android2019.ch2activity;

import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import net.zjitc.android2019.R;

import java.text.DecimalFormat;

public class BMIActivity extends AppCompatActivity {
    private EditText ed_height, ed_weight;
    private Button btn_computer, btn_about, btn_exit;
    private TextView tv_result;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bmi);
        init();
        View.OnClickListener click = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                /*View v 这个参数表示点击的是哪个控件*/
                switch (v.getId()){
                    case R.id.ch2_bmi_btn_computer:
                        computer();
                        break;
                    case R.id.ch2_bmi_btn_about:
                        about();
                        break;
                    case R.id.ch2_bmi_btn_exit:
                        exit();
                        break;
                }
            }
        };
        btn_computer.setOnClickListener(click);
        btn_exit.setOnClickListener(click);
        btn_about.setOnClickListener(click);
    }

    private void exit() {
        DialogInterface.OnClickListener listener = new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                /*关闭当前Activity*/
                BMIActivity.this.finish();
            }
        };

        //1. 创建一个AlertDialog对象
        //2.设置对话框标题
        //3.设置对话框显示的内容
        //4.添加对话框的 确定 取消 按钮
        //5.显示对话框

        AlertDialog.Builder dialog = new AlertDialog.Builder(BMIActivity.this);
        dialog.setTitle("退出？");
        dialog.setMessage("你是不是点错了？");
        dialog.setPositiveButton("滚",listener);
        dialog.setNegativeButton("点错了",null);


        dialog.show();

    }

    private void computer() {
        /*先获取输入框的内容，再根据公式计算BMI，根据BMI的值判断过轻或正常，最后显示*/
        String s_height=ed_height.getText().toString();
        double d_height=Double.parseDouble(s_height);

        String s_weight=ed_weight.getText().toString();
        double d_weight=Double.parseDouble(s_weight);

        double bmi = d_weight/(d_height/100*d_height/100);

        /*结果保留2位小数*/
        DecimalFormat f1 = new DecimalFormat("0.00");
        String s = "您的体重指数为："+f1.format(bmi)+"\n理想指数为20-22\n";
        if (bmi<18.5){
            s+="过轻";
        }else if (bmi<23){
            s+="正常";
        }else if (bmi<25){
            s+="过重";
        }else if (bmi<30){
            s+="肥胖";
        }else {
            s+="痴肥";
        }
        tv_result.setText(s);
    }
    private void about(){
        /*从BMI跳到about*/
        Intent intent = new Intent(BMIActivity.this,about.class);
        /*传递数据*/
        intent.putExtra("ykc","123");
        startActivity(intent);
    }

    private void init() {
        ed_height=findViewById(R.id.ch2_bmi_ed_height);
        ed_weight=findViewById(R.id.ch2_bmi_ed_weight);
        btn_computer=findViewById(R.id.ch2_bmi_btn_computer);
        btn_about=findViewById(R.id.ch2_bmi_btn_about);
        btn_exit=findViewById(R.id.ch2_bmi_btn_exit);
        tv_result=findViewById(R.id.ch2_bmi_tv_result);
    }
}
